package com.jie.files.shiro.realm;

import com.jie.files.entity.User;
import com.jie.files.service.UserService;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author Jeson
 * @description
 * @create 2021/10/7
 * @since 1.0.0
 */
public class MyRealm extends AuthorizingRealm {

    private final String SALT="@#44!$";

    @Autowired
    private UserService userService;
    /**
     * 授权
     * @param principalCollection
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {

        return null;
    }

    /**
     * 认证
     * @param authenticationToken
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        String principal = (String) authenticationToken.getPrincipal();
        System.out.println(principal);
       User user= userService.findUserByUsernmae(principal);

       if(user != null){
           SimpleAuthenticationInfo simpleAuthenticationInfo=
                   new SimpleAuthenticationInfo(principal,user.getPassword(), ByteSource.Util.bytes(SALT),this.getName());
           System.out.println("______________认证_________________");
           return simpleAuthenticationInfo;
       }

        return null;

    }
}